A Text File


Text File

A very common type of file is a text file. There are three common types of text files: ASCII, UNICODE and UTF8. The traditional old data files are ASCII files; they have a limited set of characters. The UNICODE text files have an ample set of characters but are not compatible with ASCII files. The UTF8 text files are commonly used in the Internet. UTF8 was created because it is compatible with the ASCII files, and because it has an ample set of characters. The program Notepad included in Microsoft Windows is capable of opening and creating files in the three formats just discussed. Most today formats such as: HTML, XML, CSS, etc., are text files.
Un tipo común de tipo de archivos es el archivo de texto. Hay tres tipos comunes de archivos de texto: ASCII, UNICODE y UTF8. Los archivos tradicionales viejos son archivos ASCII; estos tienen un conjunto de caracteres limitado. Los archivos de texto UNICODE tienen un amplio conjunto de caracteres per no son compatibles con los archivos ASCII. Los archivos de text UTF8 son usados comúnmente en la Internet. UTF8 fue creado porque es compatible con los archivos ASCII y porque tienen un conjunto amplio de caracteres. El programa de Block de Notas incluido en Microsoft Windows es capaz de abrir y crear archivos en los tres formatos que se acaban de discutir. La mayoría de los formatos de hoy tales como: HTML, XML, CSS, etc., son archivos de texto.

Problem 1
Write a program called ListNumb that writes the numbers from 1000 to 1010 in an UNICODE text file. Once the program has written the file, you may use notepad to check the contents for file.
Escriba un programa llamado ListNumb que escriba los números del 1000 al 1010 en un archivo de texto UNICODE. Una vez que el programa ha escrito el archivo, usted puede usar el block de notas para verificar el contenido del archivo.

ListNumb0

ListNumb.cpp
void ListNumb::Window_Open(Win::Event& e)
{
}

void ListNumb::btSave_Click(Win::Event& e)
{
     //____________________________________ Ask the user for the filename
     Win::FileDlg dlg;
     dlg.SetFilter(L"Unicode Text (*.txt)\0*.txt\0\0", 0, L"txt");
     if (dlg.BeginDialog(hWnd, L"Save Unicode text", true) == true)
     {
          //_____________________________ Create the file
          Sys::File file;
          if (file.CreateForWritting(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Save");
               return;
          }
          //___________________________ Write text to the file
          wstring text;
          for(int i = 1000; i <= 1010; i++)
          {
               Sys::Format(text, L"%d\r\n", i);
               file.WriteText(text);
          }
     }
}

ListNumb1

Tip
You can check the file create by the previous program is Unicode using Notepad as shown below using the option: File > Save As.
Usted puede verificar que el archivo creado por el programa previo es Unicode usando el block de notas como se muestra debajo usando el opción: Abrilr > Guardar Como.

CheckUnicode

Closing a File

After writing the content of a file, a file must be closed using the method File.Close(). Note that when the File object is destroyed, the file will be automatically close. So in most cases you do need to close the file explicitly. However, there may be some cases where you need to close a file before the file object is destroyed.
Después de escribir el contenido de un archivo, un archivo debe ser cerrado usando el método File.Close(). Note que cuando el objeto del archivo es destruido, el archivo se cerrará automáticamente. Así en la mayoría de los casos usted no necesita cerrar el archivo en forma explícita. Sin embargo, puede haber algunos casos donde usted necesite cerrar el archivo antes de que el objeto de File sea destruido.

Problem 2
Write a program called Table to generate a text file with the sentence "México is in América" in several encodings as shown. Use Notepad to verify that the files generated are in the right encoding.
Escriba un programa llamado Table para generar un archivo de texto con la frase: "México is in América" en varias codificaciones como se muestra. Use el block de notas para verificar que los archivos generaron en la codificación correcta.

Table

Table.cpp
void Table::Window_Open(Win::Event& e)
{
     this->radioUtf8.Checked = true;
}

void Table::btSave_Click(Win::Event& e)
{
     Win::FileDlg dlg;
     dlg.SetFilter(L"Text Document(*.txt)\0*.txt\0\0", 0, L"txt");
     if (dlg.BeginDialog(hWnd, L"Save text", true) == true)
     {
          Sys::File file;
          if (file.CreateForWritting(dlg.GetFileNameFullPath()) == false)
          {
               Sys::DisplayLastError(hWnd, L"Save");
               return;
          }
          //_________________________ We are ready to write
          wstring text(L"México is in América");
          string tmp;

          if (radioAscii.Checked == true)
          {
               Sys::Convert::WstringToString(text, tmp);
               file.WriteText(tmp);
          }
          else if (radioUtf8.Checked == true)
          {
               Sys::Convert::WstringToUTF8(text, tmp);
               file.WriteText(tmp);
          }
          else
          {
               // Unicode: conversion is not required
               file.WriteText(text);
          }
     }
}

Problem 3
Create a project called MyPad to display in a textbox the contents of ASCII text file.
Cree un proyecto llamado MyPad para mostrar en una caja de texto el contenido de un archivo de texto ASCII.

MyPad

MyPad.cpp
void MyPad::Window_Open(Win::Event& e)
{
}

void MyPad::btOpen_Click(Win::Event& e)
{
     Win::FileDlg dlg;
     dlg.SetFilter(L"Text Documents (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0", 0, L"txt");
     if (dlg.BeginDialog(hWnd, L"Open a ASCII text file", false) == true)
     {
          //__________________________________________ Read the file
          string asciiText;
          Sys::FileAssistant::TextLoad(dlg.GetFileNameFullPath(), asciiText);
          //__________________________________________ Convert from ASCII to Unicode
          wstring text;
          Sys::Convert::StringToWstring(asciiText, text);
          this->tbx1.Text = text;
     }
}

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home